<?php
// +----------------------------------------------------------------------
// | Fanwe 方维众筹商业系统
// +----------------------------------------------------------------------
// | Copyright (c) 2011 http://www.fanwe.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 甘味人生(526130@qq.com)
// +----------------------------------------------------------------------

$payment_lang = array(
    'name'           => '支付宝即时到帐支付',
    'alipay_partner' => '合作者身份ID',
    'alipay_account' => '支付宝帐号',
    'alipay_key'     => '校验码',
    'alipay_service' => '接口方式',
    'GO_TO_PAY'      => '前往支付宝在线支付',
    'VALID_ERROR'    => '支付验证失败',
    'PAY_FAILED'     => '支付失败',
);
$config = array(
    'alipay_partner' => array(
        'INPUT_TYPE' => '0',
    ), //合作者身份ID
    'alipay_account' => array(
        'INPUT_TYPE' => '0',
    ), //支付宝帐号:
    'alipay_key'     => array(
        'INPUT_TYPE' => '0',
    ), //校验码
);
/* 模块的基本信息 */
if (isset($read_modules) && $read_modules == true) {
    $module['class_name'] = 'Alipay';

    /* 名称 */
    $module['name'] = $payment_lang['name'];

    /* 支付方式：1：在线支付；0：线下支付 */
    $module['online_pay'] = '1';

    /* 配送 */
    $module['config'] = $config;

    $module['lang']    = $payment_lang;
    $module['reg_url'] = 'https://b.alipay.com/order/slaverIndex.htm?customer_external_id=C4393319516691172112&market_type=from_agent_contract&pro_codes=61F99645EC0DC4380ADE569DD132AD7A';
    return $module;
}
// 支付宝支付模型
require_once APP_ROOT_PATH . 'system/libs/payment.php';
class Alipay_payment implements payment
{
    public function get_payment_code($payment_notice_id)
    {
        $payment_notice         = $GLOBALS['db']->getRow("select * from " . DB_PREFIX . "payment_notice where id = " . $payment_notice_id);
        $money                  = round($payment_notice['money'], 2);
        $payment_info           = $GLOBALS['db']->getRow("select id,config,logo from " . DB_PREFIX . "payment where id=" . intval($payment_notice['payment_id']));
        $payment_info['config'] = unserialize($payment_info['config']);

        $subject = $payment_notice['deal_name'] == "" ? "充值" . format_price($payment_notice['money']) : $payment_notice['deal_name'];

        // $data_return_url = get_domain() . APP_ROOT . '/index.php?ctl=payment&act=response&class_name=Alipay';
        // $data_notify_url = get_domain() . APP_ROOT . '/index.php?ctl=payment&act=notify&class_name=Alipay';
        $data_return_url = SITE_DOMAIN . '/index.php?ctl=user&act=userinfo';
        $data_notify_url = SITE_DOMAIN . '/callback/payment/alipay_notify.php';
        //$real_method = $payment_info['config']['alipay_service'];
        $real_method = '2';

        switch ($real_method) {
            case '0':
                $service = 'trade_create_by_buyer';
                break;
            case '1':
                $service = 'create_partner_trade_by_buyer';
                break;
            case '2':
                $service = 'create_direct_pay_by_user';
                break;
        }

        $parameter = array(
            'service'           => $service,
            'partner'           => $payment_info['config']['alipay_partner'],
            //'partner'           => ALIPAY_ID,
            '_input_charset'    => 'utf-8',
            'notify_url'        => $data_notify_url,
            'return_url'        => $data_return_url,
            /* 业务参数 */
            'subject'           => $subject,
            'out_trade_no'      => $payment_notice['notice_sn'],
            'price'             => $money,
            'quantity'          => 1,
            'payment_type'      => 1,
            /* 物流参数 */
            'logistics_type'    => 'EXPRESS',
            'logistics_fee'     => 0,
            'logistics_payment' => 'BUYER_PAY_AFTER_RECEIVE',
            'extend_param'      => 'isv^fanwe11',
            /* 买卖双方信息 */
            'seller_email'      => $payment_info['config']['alipay_account'],
        );

        ksort($parameter);
        reset($parameter);

        $param = '';
        $sign  = '';

        foreach ($parameter as $key => $val) {
            $param .= "$key=" . urlencode($val) . "&";
            $sign .= "$key=$val&";
        }

        $param    = substr($param, 0, -1);
        $sign     = substr($sign, 0, -1) . $payment_info['config']['alipay_key'];
        $sign_md5 = md5($sign);

        return array(
            'status' => 1,
            'url'    => "https://www.alipay.com/cooperate/gateway.do?{$param}&sign={$sign_md5}&sign_type=MD5",
            'method' => 'new_tab',
        );

        // $payLinks = '<form action="https://www.alipay.com/cooperate/gateway.do?' . $param . '&sign=' . $sign_md5 . '&sign_type=MD5" id="jumplink" method="post">正在连接支付接口...</form>';
        // $payLinks .= '<script type="text/javascript">document.getElementById("jumplink").submit();</script>';

        // return $payLinks;
    }
    public function notify($request)
    {
        $return_res = array(
            'info'   => '',
            'status' => false,
        );
        $payment           = $GLOBALS['db']->getRow("select id,config from " . DB_PREFIX . "payment where class_name='Alipay'");
        $payment['config'] = unserialize($payment['config']);

        /* 检查数字签名是否正确 */
        ksort($request);
        reset($request);

        foreach ($request as $key => $val) {
            if ($key != 'sign' && $key != 'sign_type' && $key != 'code' && $key != 'class_name' && $key != 'act' && $key != 'ctl') {
                $sign .= "$key=$val&";
            }
        }

        $sign = substr($sign, 0, -1) . $payment['config']['alipay_key'];

        if (md5($sign) != $request['sign']) {
            echo "fail";
            die;
        }

        $payment_notice_sn = $request['out_trade_no'];

        $money           = $request['total_fee'];
        $outer_notice_sn = $request['trade_no'];

        if ($request['trade_status'] == 'TRADE_SUCCESS' || $request['trade_status'] == 'TRADE_FINISHED' || $request['trade_status'] == 'WAIT_SELLER_SEND_GOODS' || $request['trade_status'] == 'WAIT_BUYER_CONFIRM_GOODS') {

            require_once APP_ROOT_PATH . "system/libs/cart.php";
            $rs = payment_paid($payment_notice_sn, $outer_notice_sn);
            echo "success";

        } else {
            echo "fail";
        }
    }
    public function response($request)
    {
        $return_res = array(
            'info'   => '',
            'status' => false,
        );
        $payment           = $GLOBALS['db']->getRow("select id,config from " . DB_PREFIX . "payment where class_name='Alipay'");
        $payment['config'] = unserialize($payment['config']);

        /* 检查数字签名是否正确 */
        ksort($request);
        reset($request);

        foreach ($request as $key => $val) {
            if ($key != 'sign' && $key != 'sign_type' && $key != 'code' && $key != 'class_name' && $key != 'act' && $key != 'ctl') {
                $sign .= "$key=$val&";
            }
        }

        $sign = substr($sign, 0, -1) . $payment['config']['alipay_key'];

        if (md5($sign) != $request['sign']) {
            showErr("签名验证失败");
        }

        $payment_notice_sn = $request['out_trade_no'];

        $money = $request['total_fee'];

        $outer_notice_sn = $request['trade_no'];

        if ($request['trade_status'] == 'TRADE_SUCCESS' || $request['trade_status'] == 'TRADE_FINISHED' || $request['trade_status'] == 'WAIT_SELLER_SEND_GOODS' || $request['trade_status'] == 'WAIT_BUYER_CONFIRM_GOODS') {

            require_once APP_ROOT_PATH . "system/libs/cart.php";
            $rs = payment_paid($payment_notice_sn, $outer_notice_sn);
            // showSuccess($rs['info'],0,$rs['jump'],0);
            echo "支付成功";
        } else {
            // showErr("支付失败",0,url("index"),1);
            echo "支付失败";
        }
    }
    public function get_display_code()
    {
        # code...
    }

}
